package cn.itsource.hbc.pet.controller;


import cn.itsource.hbc.org.domain.Employee;
import cn.itsource.hbc.org.domain.Shop;
import cn.itsource.hbc.org.service.IShopService;
import cn.itsource.hbc.org.service.impl.ShopServiceImpl;
import cn.itsource.hbc.pet.domain.SearchMasterMsg;
import cn.itsource.hbc.pet.query.SearchMasterMsgQuery;
import cn.itsource.hbc.pet.service.ISearchMasterMsgService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.LoginContext;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {


    @Autowired
    private IShopService shopService;

    @Autowired
   private ISearchMasterMsgService searchMasterMsgService;


    @PostMapping("/publish")
    public AjaxResult publish (@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){

        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }


    }



    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){

        try {
            if(searchMasterMsg.getId()!=null){
                searchMasterMsgService.update(searchMasterMsg);
            }else {
                searchMasterMsgService.save(searchMasterMsg);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        }

    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+ e.getMessage());

        }


    }


    @GetMapping("/{id}")
    public SearchMasterMsg loadById(@PathVariable("id") Long id){

        return searchMasterMsgService.loadById(id);
    }


    @GetMapping
    public List<SearchMasterMsg> loadAll(){

        return searchMasterMsgService.loadAll();
    }

    @PostMapping
    public PageResult<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query ,HttpServletRequest request){

        Employee employee =(Employee) LoginContext.getCurrent(request);

        Shop shop = shopService.loadByAdminId(employee.getId());

        query.setShopId(shop.getId());
        return searchMasterMsgService.queryPage(query);
    }


    @PatchMapping
    public AjaxResult allDelete(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            searchMasterMsgService.allDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }














}
